#ifndef __U_TIMER_HPP__
#define __U_TIMER_HPP__

#include <ctime>
#include <stack>

namespace u {
    
    std::stack<clock_t> timers;
    
    class timer {
        public:
        template <class Fun, class...Args>
        static double run(Fun fun, Args...args) {
            clock_t start = clock();
            fun(args...);
            clock_t diff = clock() - start;
            return (static_cast<double>(diff) / CLOCKS_PER_SEC);
        }
        
        static void begin() {
            timers.push(clock());
        }
        
        static double end() {
            clock_t t = clock() - timers.top();
            timers.pop();
            return (static_cast<double>(t) / CLOCKS_PER_SEC);
        }
        
        static void clean() {
            while(! timers.empty()) {
                timers.pop();
            }
        }
    };
}

#endif
